package frames;

import getData.GetDataFromFieldsOfFramesXML;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class WriteGroupsToXMLwithFrames {

	private static List<String> listOfKeysForXML;

	@SuppressWarnings("unchecked")
	public void rewriteXMLlistOfFramesWithNewFrames(
			List<String> listOfAllAlreadyAddedFieldsInThisFrame,
			String[] listofFieldsAlreadyaddedInFrame, String nameOfXMLForFrame, String nameOfFrameWhereWeWillBeManagingFields) {
				
			GetDataFromFieldsOfFramesXML getDataFromFieldsOfFramesXML = new GetDataFromFieldsOfFramesXML();
			listOfKeysForXML = getDataFromFieldsOfFramesXML.getSetOfKeysForFrames(nameOfXMLForFrame);
			listOfKeysForXML.add(0, "ORDEROFFIELDS");		
			List<String> listOfFields = new ArrayList<String>();		
			listOfFields.clear();
						
			try{  
			Properties properties = new Properties();
			for (String item:listOfKeysForXML) {
				System.out.println("item " + item);	
				listOfFields = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
				
			String[] strarray = listOfFields.toArray(new String[0]);
			//System.out.println("strarray " + strarray[0]);
			if (item.equals(nameOfFrameWhereWeWillBeManagingFields)){
				System.out.println("SUCCESS");
				properties.setProperty(item, Arrays.toString(listofFieldsAlreadyaddedInFrame));
				}
				else  {
				properties.setProperty(item, Arrays.toString(strarray));
				}
			}
	
File file = new File(nameOfXMLForFrame);
FileOutputStream fileOut = new FileOutputStream(file);
properties.storeToXML(fileOut, nameOfFrameWhereWeWillBeManagingFields);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}	
}
	/**
	 * @param nameOfFrameWhereWeWillBeManagingFields 
	 * @param comment 
	 * @param fieldTypeString 
	 * @param howToFill 
	 * @param couldBeNull 
	 * @param unique 
	 * @param editable 
	 * @param appointment 
	 * @param nameOfField 
	 * @param args
	 */

	@SuppressWarnings("unchecked")
	public void rewriteXMLlistOfFramesWithEditedFields(
			String nameOfXMLForFrame, int numberOfRowInLine, 
			String nameOfFrameWhereWeWillBeManagingFields, String nameOfField, String appointment, String editable,
			String unique, String couldBeNull, String howToFill, String fieldTypeString, String comment) {
		
		GetDataFromFieldsOfFramesXML getDataFromFieldsOfFramesXML = new GetDataFromFieldsOfFramesXML();
		//DetailFrame detailFrame = new DetailFrame();
		listOfKeysForXML = getDataFromFieldsOfFramesXML.getSetOfKeysForFrames(nameOfXMLForFrame);

		List<String> listOfPropertiesOfField = new ArrayList<String>();		
		listOfPropertiesOfField.clear();
		try{
		Properties properties = new Properties();
		//listOfKeysForXML.add("ORDEROFFIELDS");
		//listOfKeysForXML = listOfKeysForXML.add(0, "ORDEROFFIELDS");
		//
		for (String item:listOfKeysForXML) {
			
			if (item.equals("Name of field")){
			listOfPropertiesOfField = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
			listOfPropertiesOfField.set(numberOfRowInLine,nameOfField);
			}
			if (item.equals("Appointment")){
			listOfPropertiesOfField = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
			listOfPropertiesOfField.set(numberOfRowInLine,appointment);
			}
			if (item.equals("Editable")){
			listOfPropertiesOfField = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
			listOfPropertiesOfField.set(numberOfRowInLine,editable);
			}
			if (item.equals("Unique")){
			listOfPropertiesOfField = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
			listOfPropertiesOfField.set(numberOfRowInLine,unique);
			}
			if (item.equals("Could be null")){
			listOfPropertiesOfField = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
			listOfPropertiesOfField.set(numberOfRowInLine,couldBeNull);
			}
			if (item.equals("How to fill")){
			listOfPropertiesOfField = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
			listOfPropertiesOfField.set(numberOfRowInLine,howToFill);
			}
			if (item.equals("Field type")){
			listOfPropertiesOfField = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
			String stringFieldType = String.valueOf(fieldTypeString);
			listOfPropertiesOfField.set(numberOfRowInLine,stringFieldType);
			}
			//if (item.equals("Logic")){
			//listOfPropertiesOfField = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
			//listOfPropertiesOfField.set(numberOfRowInLine,logic);
			//}
			if (item.equals("Comment")){
			listOfPropertiesOfField = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
			listOfPropertiesOfField.set(numberOfRowInLine,comment);
			}
							
			listOfPropertiesOfField = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
			listOfPropertiesOfField.set(numberOfRowInLine,comment);
			
			String[] strarray = listOfPropertiesOfField.toArray(new String[0]);
			properties.setProperty(item, Arrays.toString(strarray));
			}
		
		File file = new File(nameOfXMLForFrame);
			FileOutputStream fileOut = new FileOutputStream(file);
			properties.storeToXML(fileOut, nameOfFrameWhereWeWillBeManagingFields);
			fileOut.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();	
			}	
		}
	
	@SuppressWarnings("unchecked")
	public static void updateXMLwithFrames(
			String nameOfXMLForFrame, String nameOfFrameWhereWeWillBeManagingFields, 
			List<String> listOfFieldsFromCurrentFrame,
			List<String> listOfAppointmentsFromCurrentFrame,
			List<String> listOfEditableFromCurrentFrame,
			List<String> listOfUniqueFromCurrentFrame,
			List<String> listOfCouldBeNullFrame,
			List<String> listOfHowToFillFromCurrentFrame, 
			List<String> listOfFieldTypeFromCurrentFrame, 
			List<String> listOfCommentsFromCurrentFrame, List<String> listOfAllAddedFramesInCurrentFrame) {
	
		GetDataFromFieldsOfFramesXML getDataFromFieldsOfFramesXML = new GetDataFromFieldsOfFramesXML();
		//DetailFrame detailFrame = new DetailFrame();
		listOfKeysForXML = getDataFromFieldsOfFramesXML.getSetOfKeysForFrames(nameOfXMLForFrame);
		listOfKeysForXML.add("ORDEROFFIELDS");
		for (String string : listOfKeysForXML) {
			System.out.println("string " + string);
			
		}
		
		List<String> listOfPropertiesOfField = new ArrayList<String>();		
		listOfPropertiesOfField.clear();
		try{
		Properties properties = new Properties();
		for (String item:listOfKeysForXML) {
			//System.out.println("item " + item);	
			//"Name of field"
			if (item.equals("Name of field")){
				properties.setProperty(item, Arrays.toString(listOfFieldsFromCurrentFrame.toArray(new String[0])));
				}
			if (item.equals("Appointment")){
				properties.setProperty(item, Arrays.toString(listOfAppointmentsFromCurrentFrame.toArray(new String[0])));
				}
			if (item.equals("Editable")){
				properties.setProperty(item, Arrays.toString(listOfEditableFromCurrentFrame.toArray(new String[0])));
				}
			if (item.equals("Unique")){
				properties.setProperty(item, Arrays.toString(listOfUniqueFromCurrentFrame.toArray(new String[0])));
				}
			if (item.equals("Could be null")){
				properties.setProperty(item, Arrays.toString(listOfCouldBeNullFrame.toArray(new String[0])));
				}
			if (item.equals("How to fill")){
				properties.setProperty(item, Arrays.toString(listOfHowToFillFromCurrentFrame.toArray(new String[0])));
				}
			if (item.equals("Field type")){
				properties.setProperty(item, Arrays.toString(listOfFieldTypeFromCurrentFrame.toArray(new String[0])));
				}
			
			for (String string : listOfAllAddedFramesInCurrentFrame) {
				System.out.println("listOfAllAddedFramesInCurrentFrame" + string);
			}
			
			
			if (item.equals("ORDEROFFIELDS")){
				properties.setProperty(item, Arrays.toString(listOfAllAddedFramesInCurrentFrame.toArray(new String[0])));
				}
			//if (item.equals("Logic")){
			//listOfPropertiesOfField = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
			//listOfPropertiesOfField.set(numberOfRowInLine,logic);
			//}
			if (item.equals("Comment")){
			properties.setProperty(item, Arrays.toString(listOfCommentsFromCurrentFrame.toArray(new String[0])));
				}
			if (!item.equals("Name of field") && !item.equals("Name of field") &&
					!item.equals("Appointment") && !item.equals("Editable") &&
					!item.equals("Unique") && !item.equals("Could be null") &&
					!item.equals("How to fill") && !item.equals("Field type")
					&& !item.equals("Comment") && !item.equals("ORDEROFFIELDS")){
				properties.setProperty(item, Arrays.toString(listOfFieldTypeFromCurrentFrame.toArray(new String[0])));
				listOfPropertiesOfField = (List<String>) getDataFromFieldsOfFramesXML.getListOfAllDataForCurrentKey(nameOfXMLForFrame,item);
				properties.setProperty(item,Arrays.toString(listOfPropertiesOfField.toArray(new String[0])));
				}
			}
		
		File file = new File(nameOfXMLForFrame);
			FileOutputStream fileOut = new FileOutputStream(file);
			properties.storeToXML(fileOut, nameOfFrameWhereWeWillBeManagingFields);
			fileOut.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();	
			}	
	
	}
	public void createNewFrame(String nameOfFrameToViewRecievedFromMainForm, String nameOfXMLForFrame) {
		
		List<String> listOfPropertiesOfField = new ArrayList<String>();		
		listOfPropertiesOfField.clear();
		try{
		Properties properties = new Properties();
		properties.setProperty ("ORDEROFFIELDS","");
		properties.setProperty ("Name of field","[Name of new field]");
		properties.setProperty ("Appointment","[Why we should add this field to our frame]");
		properties.setProperty ("Editable","[true]");
		properties.setProperty ("Unique","[true]");
		properties.setProperty ("Could be null","[true]");
		properties.setProperty ("How to fill","[How user or system will input data to this field]");
		properties.setProperty ("Field type","[text]");
		//properties.setProperty ("Logic","");
		properties.setProperty ("Comment","[Comments about this new fields]");
		
			File file = new File(nameOfXMLForFrame);
			FileOutputStream fileOut = new FileOutputStream(file);
			properties.storeToXML(fileOut, nameOfFrameToViewRecievedFromMainForm);
			fileOut.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();	
			}	
	}
		
}



